package com.project.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyFactory  implements InvocationHandler {
    private Object target;
    public ProxyFactory(Object target){
        this.target = target;
    }

    public Object getProxy(){
        //代理对象执行方法时，都会调用invoke方法
        return Proxy.newProxyInstance(target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                this);
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("方法执行前");

        //执行目标对象的目标方法，得到方法执行后的返回值
        Object o =  method.invoke(target);

        System.out.println("方法执行后");

        return o;

    }
}
